/*子应用*/
const express = require("express");

const query = require("../pool");

const vertify = require("../modules/token");

const product = express.Router();
//搜索页面的热点数据的获取
product.get("/hotspot", async (req, res) => {
  let result = await query(
    `select * from cakelist where cakelist.family="蛋糕"  order by RAND() limit 10;`
  );
  // console.log(result);
  if (result.length == 0) {
    res.send({ code: 0, msg: "未查询到相关结果" });
  } else {
    res.send({ code: 1, msg: "搜索成功", data: result });
  }
});

//模糊查询的提示信息
product.get("/vague", async (req, res) => {
  let obj = req.query;
  // console.log(obj.name);
  let params = obj.name;

  //  select * from cakelist left join taste_type on cakelist.taste_type=taste_type.typeid left join discount_type on cakelist.discount_type=discount_type.did;
  let result = await query(
    `select * from cakelist  left join taste_type on cakelist.taste_type=taste_type.typeid where cakename like '%${params}%' and cakelist.family="蛋糕" ; `
  );
  // console.log(result);
  if (params.length == 0) {
    res.send({ code: 0, msg: "未查询到相关结果" });
  } else {
    res.send({ code: 1, msg: "搜索成功", data: result });
  }
});
// 遍历所有的历史记录
product.get("/history", async (req, res) => {
  let obj = req.query;
  let uid = vertify.verifytoken(obj.token);
//console.log(obj)
//console.log(313131333111333331)
  if(uid.uid==undefined){
    result={content:"请先登录"}
    res.send({ code: 1, msg: "添加成功", data: result });
  }else{
      let result = await query(
      `select distinct content  from historical_records where number=${uid.uid} ; `
    );
  //console.log(result)
    if (result.length == 0) {
      res.send({ code: 0, msg: "没有添加成功" });
    } else {
      res.send({ code: 1, msg: "添加成功", data: result });
    }
  }
  
  
});
// 存放历史记录
product.get("/content", async (req, res) => {
  let obj = req.query;
  let uid = vertify.verifytoken(obj.token);
  let searchcursom = obj.searchcursom;
  // console.log(uid);
  let result = await query(
    `insert into historical_records(content,number) values('${searchcursom}','${uid.uid}');`
  );
  if (result.length == 0) {
    res.send({ code: 0, msg: "没有添加成功" });
  } else {
    res.send({ code: 1, msg: "添加成功", data: result });
  }
});
// 删除所有的数据
product.get("/delete", async (req, res) => {
  let obj = req.query;
  let uid = vertify.verifytoken(obj.token);
  // console.log(uid);
  let result = await query(
    `delete from historical_records where number=${uid.uid};`
  );
  if (result.length == 0) {
    res.send({ code: 0, msg: "没有删除成功" });
  } else {
    res.send({ code: 1, msg: "删除成功", data: result });
  }
});
//查看是否是正确的搜索内容
product.get("/yes_no", async (req, res) => {
  let obj = req.query;
//console.log("asdfasdfasdfasdfasdf")
//console.log(obj)
  let result = await query(
    `select * from cakelist where cakename="${obj.contents}";`
  );
  if (result.length == 0) {
    res.send({ code: 0, msg: "没有检测到" });
  } else {
    res.send({ code: 1, msg: "检测到了数据", data: result });
  }
});
//暴露
module.exports = product;
